package com.qd.flow.async;

import com.qd.common.flow.domain.event.FlowTaskStateDTO;
import com.qd.flow.event.ApprovalStateEvent;
import com.qd.flow.listener.FlowListener;
import com.qd.flow.manager.FlowListenerManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步处理
 *
 * @author sjk
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class FlowAsyncService {

    /**
     * 审批状态回执
     *
     * @param event 审批状态
     */
    @Async
    @EventListener(classes = ApprovalStateEvent.class)
    public void approvalStateEvent(ApprovalStateEvent event) {
        final FlowTaskStateDTO flowTaskStateDTO = (FlowTaskStateDTO) event.getSource();
        log.info("审批状态回执:processCode: {},bid: {},flowEventType: {}", flowTaskStateDTO.getProcessCode(), flowTaskStateDTO.getBid(), flowTaskStateDTO.getFlowEventType().name());
        FlowListener flowListener = FlowListenerManager.getInstance().getFlowListener();
        if (null != flowListener) {
            flowListener.notify(() -> flowTaskStateDTO);
        }
        flowTaskStateDTO.freeData();
    }
}